package com.googlecode.spruce.web.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.spruce.dao.OperatingSystemDao;
import com.googlecode.spruce.domain.OperatingSystem;

@Controller
@RequestMapping("/v1/os")
public class OperatingSystemController
{

    @Autowired
    private OperatingSystemDao operatingSystemDao;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody
    OperatingSystem getById(@PathVariable Integer id)
    {
        return operatingSystemDao.findByID(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public @ResponseBody
    Integer create(final @RequestBody OperatingSystem os)
    {
        Assert.notNull(os);
        Assert.hasText(os.getProduct());
        Assert.hasText(os.getVendor());
        Assert.hasText(os.getVersion());

        return operatingSystemDao.save(os);
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody
    List<OperatingSystem> getAll()
    {
        return operatingSystemDao.getAll();
    }
}
